package com.gxwzxy.medic.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxwzxy.medic.entity.Area;
import com.gxwzxy.medic.entity.Institution;
import com.gxwzxy.medic.service.AreaService;
import com.gxwzxy.medic.service.InstitutionService;
import com.gxwzxy.medic.service.Impl.AreaServiceImpl;
import com.gxwzxy.medic.service.Impl.InstitutionServiceImpl;
import com.gxwzxy.medic.until.PageBean;

/**
 * Servlet implementation class InstitutionServlet
 */
public class InstitutionServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
    
	/**
     * 农合经办机构分页展示
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
	public void findInstitution (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		//获取响应数据
    	String sindex = request.getParameter("index");
    	int index = 1;
		try {
    		index = Integer.parseInt(sindex);
    	}catch(NumberFormatException n) {
    		n.printStackTrace();
    	}
    	//查询所有行政区域
		AreaService as = new AreaServiceImpl();
		List<Area> la = as.findAllArea();
		//分页
    	PageBean<Institution> pu = new PageBean<Institution>();
    	pu.setIndex(index);//设置展示的当前页数
    	InstitutionService is = new InstitutionServiceImpl();
    	is.pageInstitution(pu);
		request.setAttribute("pu", pu);
		request.setAttribute("la", la);
		request.getRequestDispatcher("page/institution/institution.jsp").forward(request, response);
	}
	
	/**
	 * 查询所有行政区域和所有农合经办点
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findInsArea(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{

		//处理请求
		//查询所有行政区域
		AreaService as = new AreaServiceImpl();
		List<Area> la = as.findAllArea();
		//查询所有经办点
		InstitutionService is = new InstitutionServiceImpl();
		List<Institution> ll = is.findAllIns();
		for(int i = 0 ;i<la.size();i++) {
			for(int j = 0 ;j<ll.size();j++) {
				if(la.get(i).getAreaCode().equals(ll.get(j).getAreaCode())) {
					la.get(i).setAreaCode("0");
				}
			}
		}
		request.setAttribute("la",la);
		request.getRequestDispatcher("page/institution/addIns.jsp").forward(request, response);
		
	}
	
	/**
	 * 添加经办机构
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void addIns(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		//获取请求数据
		String area = request.getParameter("Ins");
		String agenName = request.getParameter("agenName");//新经办机构名称
		//处理请求
		//将option的value值分割
		String [] areaA = area.split(",");
		String areaCode= areaA[0];//所属的行政区域编号
		String areagrade = areaA[1];//所属的行政区域等级
		//添加新经办机构
		InstitutionService is = new InstitutionServiceImpl();
		if(is.saveIns(agenName,areaCode,areagrade)){
			request.setAttribute("url", "InstitutionServlet?method=findInstitution&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
	
	/**
	 * 根据agenCode修改经办机构
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void updateInsByagenCode(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		//获取请求数据
		String agenCode = request.getParameter("agenCode");
		String agenName = request.getParameter("agenName");
		//处理请求
		InstitutionService is = new InstitutionServiceImpl();
		if(is.updateIns(agenCode,agenName)) {
			request.setAttribute("url", "InstitutionServlet?method=findInstitution&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
	
	/**
	 * 根据agenCode删除经办点
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void deleteIns(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		//获取请求数据
		String agenCode = request.getParameter("agenCode");
		//处理请求
		InstitutionService is = new InstitutionServiceImpl();
		if(is.deleteIns(agenCode)) {
			response.getWriter().write("true");
		}
	}
}
